package com.ncloud.resman.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.body.Result;
import com.ncloud.common.utils.ResultUtil;
import com.ncloud.resman.entity.Brand;
import com.ncloud.resman.service.BrandService;
import com.ncloud.resman.service.transform.BrandTransService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * Created by gaowei on 2017/7/27.
 */
@Api(value = "自动化运维用户管理", description = "品牌管理", tags = "品牌管理",
basePath="/",consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/resman")
public class BrandController {
	@Autowired
	private BrandService brandService;
	@Autowired
	private BrandTransService transService;
	
	@ApiOperation(value="list brands", notes="获取基础品牌列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
		@ApiImplicitParam(paramType="query", name = "eqptType", value = "设备类型", required = false, dataType = "String" ),
		@ApiImplicitParam(paramType="query", name = "pageSize", value = "每页数量", required = false, dataType = "Long" ),
        @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = false, dataType = "Long" )
	})
    @GetMapping(value = "/brands")
    public Result<?> getBrandList(@RequestParam(required=false) String eqptType, @RequestParam(required=false) Long pageSize,
    		@RequestParam(required=false) Long pageNum,HttpServletRequest request){
		JSONObject json = new JSONObject();
		if(StringUtils.isNotBlank(eqptType)){
			json.put("eqptType", eqptType);
		}
		if(pageSize != null && pageNum != null){
			json.put("pageSize", pageSize);
			json.put("pageNum", pageNum);
		}
    	Object brandList = brandService.getBrandList(json);
		return ResultUtil.success(brandList);
    }
	
	@ApiOperation(value="brand", notes="获取基础品牌信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
        @ApiImplicitParam(paramType="path",name = "brandId", value = "品牌编码", required = true, dataType = "Long" )
	})
    @GetMapping(value = "/brand/{brandId}")
    public Result<?> getBrandInfo(@PathVariable Long brandId,HttpServletRequest request){
		Brand brand = brandService.getBrandById(brandId);
		return ResultUtil.success(brand);
    }
	
	@ApiOperation(value="add brand", notes="创建基础品牌信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
        @ApiImplicitParam(name = "brand", value = "品牌信息", required = true, dataType = "Brand" )
	})
    @PostMapping(value = "/brand")
    public Result<?> add(HttpServletRequest request, @RequestBody Brand brand){
    	Object userID = request.getAttribute("userID");
    	Long userId = Long.parseLong(userID.toString());
    	brand.setCreateUser(userId);
    	brand.setCreateDate(new Date());
    	brand.setCreateDate(new Date());
    	brand.setCreateUser(userId);
    	brandService.insertBrand(brand);
		return ResultUtil.success();
    }
    
	@ApiOperation(value="update brand", notes="更新基础品牌信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
        @ApiImplicitParam(name = "brand", value = "品牌信息", required = true, dataType = "Brand" )
	})
    @PutMapping(value = "/brand")
    public Result<?> update(HttpServletRequest request, @RequestBody Brand brand){
    	Object userID = request.getAttribute("userID");
    	brand.setUpdateUser(Long.parseLong(userID.toString()));
    	brand.setUpdateDate(new Date());
    	brandService.updateBrand(brand);
		return ResultUtil.success();
    }
    
	@ApiOperation(value="delete brand", notes="删除基础品牌信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
        @ApiImplicitParam(paramType="path", name = "brandId", value = "品牌编码", required = true, dataType = "Long" )
	})
    @DeleteMapping(value = "/brand/{brandId}")
    public Result<?> delete(HttpServletRequest request, @PathVariable("brandId") Long brandId){
		Brand brand = new Brand();
		brand.setBrandId(brandId);
		Object userID = request.getAttribute("userID");
    	brand.setUpdateUser(Long.parseLong(userID.toString()));
    	brand.setUpdateDate(new Date());
    	brandService.deleteBrand(brand);
		return ResultUtil.success();
    }
	
	@ApiOperation(value="import brand list", notes="导入品牌列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
		@ApiImplicitParam(paramType="form",name = "file", value = "excel附件", required = true, dataType = "MultipartFile" )
	})
    @PostMapping(value = "/brands/import")
    public Result<?> importBrand(HttpServletRequest request, @RequestParam(value="file",required=false) MultipartFile file) throws IOException{
    	Object userID = request.getAttribute("userID");
    	Long userId = Long.parseLong(userID.toString());
    	transService.importBrand(userId,file.getInputStream());
		return ResultUtil.success();
    }
	
	@ApiOperation(value="export brand list", notes="导入品牌列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
		@ApiImplicitParam(paramType="query",name = "eqptType", value = "设备类型", required = false, dataType = "String" )
	})
    @GetMapping(value = "/brands/export")
    public Result<?> exportBrand(@RequestParam(value="eqptType",required=false) String eqptType,
    		HttpServletRequest request,HttpServletResponse response) throws IOException{
    	transService.exportBrand(eqptType,response,false);
		return ResultUtil.success();
    }
	
	@ApiOperation(value="export brand template", notes="导入品牌列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="header",name = "X-CSRF-TOKEN", value = "认证token", required = true, dataType = "String" ),
		@ApiImplicitParam(paramType="query",name = "eqptType", value = "设备类型", required = false, dataType = "String" )
	})
    @GetMapping(value = "/brands/exportTemplate")
    public Result<?> exportBrandTemplate(HttpServletRequest request,HttpServletResponse response) throws IOException{
    	transService.exportBrand(null,response,true);
		return ResultUtil.success();
    }
}
